(function() { 
    'use strict';
    
    var XHRLive = function() { 
	var mId  = "log";
	this.mLog = null;
	this.mTable = null;
	// this.URL = "http://lm.gss.dr.dk/V/V01H.stream/Playlist.m3u8";
	// this.URL = "http://dr05-lh.akamaihd.net/z/dr05_0@147058/manifest.f4m?b=300-1600";
	this.URL = "http://dr02-lh.akamaihd.net/i/dr02_0@147055/master.m3u8?b=100-2000&rebase=on";
	this.setLog = function() { 
	    this.mLog = document.getElementById(mId);
	    this.mTable = document.getElementById("table");
	};
	this.appendMsg = function(txt)  { 
	    this.mLog.innerHTML += txt + "\n";
	};
	this.initialize();
    };
    
    XHRLive.prototype.initialize = function() {
	this.setLog();
	this.getChannels(true);
	// this.testDirect();
	// this.testAjax(true);
    }
    XHRLive.prototype.getChannels = function(wrapper) {
	var u = "http://www.dr.dk/tv/external/channels?mediaType=tv";
	if (wrapper) {
	    this.testAjax(u,this.showChannels.bind(this));
	} else {
	    this.testDirect(u,this.showChannels.bind(this));
	}
    }
    XHRLive.prototype.showChannels = function(data) { 
	this.appendMsg("Result:\n" + JSON.stringify(data, null, "  "));
	var now = new Date();
	console.log("Now: " + now.valueOf() + " " + now.toString());
	var top = data["Data"];
	var txt = "";
	for (var ich in top) { 
	    var ch = top[ich];
	    if (ch["ChannelType"] != 0 || ch["WebChannel"] == true) 
		continue;
	    var tit  = ch["Title"];
	    var slug = ch["Slug"];
	    txt += "<tr><th colspan='2'>" + tit + "</th>"
		+  "<th colspan='3'>" + slug + "</th></tr>";
	    
	    var servers = ch["StreamingServers"];
	    for (var isrv in servers) { 
		var srv  = servers[isrv];
		var base = srv["Server"];
		var type = srv["LinkType"];
		txt += "<tr><td></td><td>" + type + "</td>";
		
		var quals = srv["Qualities"];
		for (var i = 0; i < quals.length; i++) { 
		    var qual = quals[i];
		    var rate = qual["Kbps"];
		    
		    if (i != 0) 
			txt += "<tr><td></td><td></td>";
		    txt += "<td>" + rate + "</td>";
		    var streams = qual["Streams"];
		    for (var istr in streams) { 
			var str = streams[istr];
			var fil = str["Stream"];
			
			txt += "<td><a href='" + base + "/" + fil + "'>" 
			    +  base + "/" + fil + "</a></td>";
			
		    } // for istr
		    txt += "</tr>";
		} // for i 
	    } // for isrv 
	} // for ich

	this.mTable.innerHTML = txt;
    }
	    
    XHRLive.prototype.testDirect = function(u,cb) {
	this.appendMsg("Direct test: " + this.URL);
	var cbf = cb || this.success.bind(this);
	XHR.direct(u == null ? this.URL : u, cbf, this.failure.bind(this));
    }
    XHRLive.prototype.testAjax = function(u,cb) {
	this.appendMsg("Ajax test");
	var cbf = cb || this.success.bind(this);
	XHR.ajax(u == null ? this.URL : u, cbf, this.failure.bind(this), false);
    }
    XHRLive.prototype.success = function(data) {
	this.appendMsg("Result:\n" + JSON.stringify(data, null, "  "));
	this.jsonTree(data);
    }
    XHRLive.prototype.failure = function() { 
	this.appendMsg("Failed!");
    }
    XHRLive.prototype.jsonTree = function(e) {
	console.log(JSON.stringify(e, null, "  "));
    }
    
    window.XHRLive = XHRLive;
})();
// 
// EOF
//
